package com.ironman.mes.plugin.ee.service;


import lombok.extern.slf4j.Slf4j;
import org.noear.dami.Dami;
import org.noear.snack.ONode;
import org.noear.solon.annotation.Component;
import org.noear.solon.core.bean.LifecycleBean;

@Slf4j
@Component
public class DataHandler implements LifecycleBean {

    private static final String TOPIC_FILTER = "thing/up/property/uz7u229b/*";

    @Override
    public void start() throws Throwable {
        log.info("DataHandler start");
        Dami.<String, Void>bus().listen(TOPIC_FILTER, payload -> {
            String content = payload.getContent();
            ONode root = ONode.loadStr(content);
            int eng = root.select("eng").getInt();
            String id = root.select("id").getString();
            String datetime = root.select("datetime").getString();
            log.info("设备ID {}, 总能耗 {} ,统计时间 {}", id, eng, datetime);
        });
    }

    @Override
    public void preStop() throws Throwable {
        log.info("DataHandler preStop");
        Dami.<String, Void>bus().unlisten(TOPIC_FILTER);
    }
}
